package com.xrtn.service.impl;

import com.xrtn.bean.SysUserInfo;
import com.xrtn.mapper.SysRoleInfoMapper;
import com.xrtn.mapper.TPatientInfoMapper;
import com.xrtn.mapper.TPatientTreatmentMapper;
import com.xrtn.page.Page;
import com.xrtn.service.ISysDoctorService;
import com.xrtn.utils.DateUtils;
import com.xrtn.utils.ObjUtils;
import com.xrtn.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by tu_pei on 2018/3/10.
 */
@Service
public class SysDoctorServiceImpl implements ISysDoctorService{

    @Autowired
    private TPatientInfoMapper patientInfoMapper;
    @Autowired
    private TPatientTreatmentMapper patientTreatmentMapper;
    @Autowired
    private SysRoleInfoMapper roleInfoMapper;

    @Override
    public Page selectSysPatientPageList(Page page,Map<String, Object> params) {

        String fromPage = (String) params.get("fromPage");
        if ("0".equals(fromPage)) { // 从宝宝库查询
            // 校验查询人员是否有查看全部门店的权限
            boolean canSeeAll = false;
            SysUserInfo loginUser = (SysUserInfo) params.get("loginUser");
            if(ObjUtils.isNotEmpty(loginUser)){

                List<Map<String,Object>> userFunctionList= roleInfoMapper.selectRoleLists(loginUser.getUserRole());
                List<String> groupList = new ArrayList<>();

                for (Map<String,Object> functionMap : userFunctionList){
                    groupList.add(functionMap.get("menuName").toString());
                    if(Integer.parseInt(functionMap.get("functionId").toString()) == 45){
                        canSeeAll = true;
                    }
                }
            }

            if (!canSeeAll) {
                params.put("seeBookingSubclinicId", loginUser.getSubclinicId());
            }
        }

        //时间分类：0，1，2，3，4：全部，一，二，三，超过三个月未到店
        Object timeType = params.get("timeType");

        if(ObjUtils.isNotEmpty(timeType)){
            if(timeType.toString().equals("1")){
                params.put("datediffDay", 30);
                params.put("timeType", 1);
            }
            if(timeType.toString().equals("2")){
                params.put("datediffDay", 60);
                params.put("timeType", 2);
            }
            if(timeType.toString().equals("3")){
                params.put("datediffDay", 90);
                params.put("timeType", 3);
            }
            if(timeType.toString().equals("4")){
                params.put("datediffDay", 91);
                params.put("timeType", 4);
            }
        }

        List<Map<String,Object>> list = new ArrayList<>();

        try{
            String str = DateUtils.format(new Date(),DateUtils.YYYY_MM_DD);
            Date date = DateUtils.parse(str,DateUtils.YYYY_MM_DD);
            Calendar dq = Calendar.getInstance();
            dq.setTime(date);

            list = patientInfoMapper.selectSysPatientPageList(page,params);
            if(list.size() > 0){
                for (Map<String,Object> map : list){
                    if(ObjUtils.isNotEmpty(map.get("patientBirthday"))){
                        String dd = map.get("patientBirthday").toString();
                        Date date2 = DateUtils.parse(dd,DateUtils.YYYY_MM_DD);
                        Calendar rz = Calendar.getInstance();
                        rz.setTime(date2);
                        map.put("patientBirthday", DateUtils.getDate(dq,rz));
                    }

                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        page.setItems(list);
        return page;
    }

    /**
     * 后台查询病例看板
     *
     * @param params
     * @return
     */
    @Override
    public ResponseData selectDiseaseBook(Map<String, Object> params) {

        ResponseData data = new ResponseData();

        Integer treatmentCount = patientTreatmentMapper.selectTreatmentCount(params);

        params.put("denominator", treatmentCount);

        List<Map<String,Object>> treatmentList = patientTreatmentMapper.selectDiseaseBook(params);

        List<Integer> typeList = new ArrayList<>();

        for (Map<String,Object> treatment : treatmentList){
            typeList.add(Integer.parseInt(treatment.get("typeId").toString()));
        }
        List<Integer> newList = new ArrayList(new HashSet(typeList));
        Object typeName = null,typeRaion = null, typeCount = null;
        List<Map<String,Object>> retList = new ArrayList<>();
        for (Integer typeId : newList){
            Map<String,Object> retMap = new HashMap<>();
            retMap.put("typeId", typeId);
            List<Map<String,Object>> groupMapList = new ArrayList<>();
            for (Map<String,Object> treatment : treatmentList){
                Integer treatmentTypeId = Integer.parseInt(treatment.get("typeId").toString());
                if(treatmentTypeId == typeId){
                    typeName = treatment.get("typeName").toString();
                    typeRaion = treatment.get("typeRaion").toString();
                    typeCount = treatment.get("typeCount").toString();
                    groupMapList.add(treatment);
                }
            }
            retMap.put("diseaseList",groupMapList);
            retMap.put("typeName",typeName);
            retMap.put("typeRaion",typeRaion);
            retMap.put("typeCount",typeCount);
            retList.add(retMap);
        }

        Collections.sort(retList, (o1, o2) -> {
            double d1 = Double.parseDouble(o1.get("typeRaion").toString());
            double d2 = Double.parseDouble(o2.get("typeRaion").toString());
            return d1 - d2 < 0 ? 1 : -1;
        });

        data.setResults(retList);

        return data;
    }
}
